using System;
using System.Collections;
using System.IO;
using System.Security.Cryptography;

namespace WPChanger
{
	/// <summary>
	/// Summary description for WPProvider.
	/// </summary>
	public class WPProvider
	{		
		private string _CurDir = "";
		private string _SubDir = "";
		private WPCache _WPCache = null;

		public WPProvider(Settings mySettings)
		{
			if (!Directory.Exists(mySettings.WPDirectory))
			{
				throw new DirectoryNotFoundException(mySettings.WPDirectory);
			}
			else
			{
				_CurDir = mySettings.WPDirectory;
				_WPCache = new WPCache(_CurDir,mySettings.PatternMatch);
			}
		}

		public string CurrentDir
		{
			get
			{
				return _CurDir;
			}
		}
		public string SubDir
		{
			get
			{
				return _SubDir;
			}
		}

		public ArrayList getFirstLevelDirectories()
		{
			ArrayList al = new ArrayList();
			DirectoryInfo di = new DirectoryInfo(_CurDir);
			foreach(DirectoryInfo _di in di.GetDirectories())
			{	
				al.Add(_di.Name);
			}		
			return al;
		}

		public ArrayList getSubLevelDirectories(string subDir)
		{
			ArrayList al = new ArrayList();
			DirectoryInfo di = new DirectoryInfo(_CurDir + @"\"+ subDir);
			foreach(DirectoryInfo _di in di.GetDirectories())
			{	
				al.Add(_di.Name);
			}		
			return al;
		}

		public ArrayList getWPs(string dir)
		{
			return new ArrayList(Directory.GetFiles(dir));
		}

		/// <summary>
		/// Return a string to a wallpaper, choosed randomly in the recursive collection of files
		/// </summary>
		/// <returns></returns>
		public string getRandomWP()
		{
			ArrayList al = _WPCache.getWPs();
			if (al.Count > 0)
			{
				Random rnd = new Random(DateTime.Now.Millisecond);
				return (string)al[rnd.Next(al.Count)];
			}
			else
			{
				return "";
			}
		}

		/// <summary>
		/// Change directory in WP selection
		/// </summary>
		/// <param name="subDir"></param>
		public void changeWPDir(string dir)
		{
			if (dir.Equals(".."))
			{
				_CurDir = Directory.GetParent(_CurDir).FullName;
			}
			else if (!dir.Equals("."))
			{
				//_CurDir = _CurDir + @"\"+ dir;
				_WPCache.changeWPDir(_CurDir + @"\"+ dir);
			}
			//_WPCache.changeWPDir(_CurDir);
		}
	}
}

